﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using System.Globalization;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem
{
    public partial class ViewUser : System.Web.UI.Page
    {
        static readonly PagedDataSource PagedDataSource = new PagedDataSource();
        static int _pageNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewPanel.Visible = true;
                EditPanel.Visible = false;

                StatusDropDownList.Items.Add("All");
                StatusDropDownList.Items.Add("IsActive");
                StatusDropDownList.Items.Add("InActive");

                StatusEditDropDownList.Items.Add("True");
                StatusEditDropDownList.Items.Add("False");
                FetchRoleName();
            }
        }

        protected void ListSorting(object sender, ListViewSortEventArgs e)
        {
            string sortColumn = e.SortExpression;
            DataTable table = UserBusiness.FetchByUserName(UserNameTextBox.Text);
            Sort(table,sortColumn,"ASC");
        }

        private void Sort(DataTable table,string sortColumn,string sortType)
        {
            DataView view = table.DefaultView;
            view.Sort = sortColumn + " "+sortType;
            PagedDataSource.DataSource = view;
            DataPaging();
        }

        private void FetchAll()
        {
            int type = 1;
            switch (StatusDropDownList.Text)
            {
                case "IsActive":
                    type = 2;
                    break;
                case "InActive":
                    type = 3;
                    break;
            }

            PagedDataSource.DataSource = UserBusiness.FetchAll(type).DefaultView;
            DataPaging();
        }

        public void FetchRoleName()
        {
            PagedDataSource.DataSource = UserBusiness.FetchByUserName(UserNameTextBox.Text).DefaultView;
            DataPaging();
        }

        private void DataPaging()
        {
            PagedDataSource.PageSize = 15;
            PagedDataSource.CurrentPageIndex = _pageNumber;
            PagedDataSource.AllowPaging = true;
            if (PagedDataSource.IsFirstPage)
            {
                FirstPageButton.Visible = false;
                PreviewPageButton.Visible = false;
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
            if (PagedDataSource.IsLastPage)
            {
                LastPageButton.Visible = false;
                NextPageButton.Visible = false;
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
            PageLabel.Text = (_pageNumber + 1) + " / " + PagedDataSource.PageCount;
            ContractListView.DataSource = PagedDataSource;
            ContractListView.DataBind();
            MessageLabel.Text = "";
            RoleDropDownList.DataSource = UserBusiness.FetchNameRole();
            RoleDropDownList.DataTextField = "RoleName";
            RoleDropDownList.DataValueField = "RoleId";
            RoleDropDownList.DataBind();
        }

        protected void DeleteOnCommand(object sender, CommandEventArgs commandEventArgs)
        {
            UserBusiness.Delete(commandEventArgs.CommandArgument.ToString());
            FetchAll();
        }

        protected void EditOnCommand(object sender, CommandEventArgs e)
        {
            ViewPanel.Visible = false;
            EditPanel.Visible = true;

            var userName = (Label)ContractListView.Items[int.Parse(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("UserNameLabel");
            UserNameEditTextBox.Text = userName.Text;

            var userId = (Label)ContractListView.Items[int.Parse(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("UserIdLabel");
            UserIdTextBox.Text = userId.Text;

            var status = (Label)ContractListView.Items[int.Parse(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("StatusLabel");
            StatusEditDropDownList.Text = status.Text;

            var role = (Label)ContractListView.Items[int.Parse(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("RoleNameId");
            RoleDropDownList.Text = role.Text;

        }

        protected void UserNameTextBoxTextChanged(object sender, EventArgs e)
        {
            FetchRoleName();
        }

        protected void SearchButtonClick(object sender, EventArgs e)
        {
            FetchRoleName();
        }

        protected void BackButton_OnClick(object sender, EventArgs e)
        {
            ViewPanel.Visible = true;
            EditPanel.Visible = false;
            MessageLabel.Text = "";
        }

        protected void UpdateButton_OnClick(object sender, EventArgs e)
        {
            var userEntity = new UserEntity
                                 {
                                     UserId = new Guid(UserIdTextBox.Text),
                                     RoleId = new Guid(RoleDropDownList.Text),
                                     IsActive = bool.Parse(StatusEditDropDownList.SelectedValue)
                                 };
            if (UserBusiness.UpdateUserByRole(userEntity))
            {
                MessageLabel.Text = "Update Success";
                ViewPanel.Visible = true;
                EditPanel.Visible = false;
            }
        }

        protected void LastPageButton_Click(object sender, EventArgs e)
        {
            _pageNumber = PagedDataSource.PageCount - 1;
            FetchRoleName();
            LastPageButton.Visible = false;
        }

        protected void FirstPageButton_Click(object sender, EventArgs e)
        {
            _pageNumber = 0;
            FetchRoleName();
            FirstPageButton.Visible = false;
        }

        protected void PreviewPageButton_Click(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsFirstPage)
            {
                _pageNumber--;
                FetchRoleName();
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
        }

        protected void NextPageButton_Click(object sender, EventArgs e)
        {
            if (!PagedDataSource.IsLastPage)
            {
                _pageNumber++;
                FetchRoleName();
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
        }

        protected void StatusDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FetchAll();
        }
    }
}
